//
//  StudyViewController.swift
//  BLH
//
//  Created by zhangshumeng on 2021/8/12.
//

import JXCategoryView

class StudyViewController: BaseViewController {

    var id: String = ""
    private var controllers = [StudyChildViewController]()
    private var titles = [StudySecItemModel]()
    private var cateId: String = ""
    private var imageBaseName = ""
    
    private lazy var titleView: JXCategoryNumberView = {
        let lineView = JXCategoryIndicatorLineView()
        lineView.indicatorColor = .theme
        lineView.indicatorHeight = 4
        let titleView = JXCategoryNumberView()
        titleView.defaultSelectedIndex = 0
        titleView.delegate = self
        titleView.indicators = [lineView]
        titleView.titleColor = .c999999
        titleView.titleSelectedColor = .theme
        titleView.titleFont = 14.font
        titleView.titleSelectedFont = 16.font
        titleView.isTitleColorGradientEnabled = true
        titleView.backgroundColor = .white
        titleView.isAverageCellSpacingEnabled = true
        titleView.cellSpacing = 0
        return titleView
    }()
    
    private lazy var listContainerView: JXCategoryListContainerView = {
        let listContainerView = JXCategoryListContainerView(type: .scrollView, delegate: self)
        listContainerView?.setDefaultSelectedIndex(0)
        return listContainerView!
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addSubviews()
        requestStudyDetailData()
    }

    private func addSubviews() {
        automaticallyAdjustsScrollViewInsets = false

        view.addSubview(titleView)
        view.addSubview(listContainerView)

        titleView.snp.makeConstraints { (make) in
            make.top.equalTo(topLayoutGuideBottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
        }

        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
            make.bottom.equalToSuperview()
        }

        titleView.contentScrollView = listContainerView.scrollView
    }

    private func reloadData() {
        titleView.titles = titles.map({
            return $0.name
        })
        for item in titles {
            let vc = StudyChildViewController()
            vc.imageName = imageBaseName + String(format: "%02ld", item.nameIndex)
            vc.cateId = cateId
            vc.secId = item.secId
            controllers.append(vc)
        }
        titleView.reloadData()
        listContainerView.reloadData()
    }
}

extension StudyViewController {
    
    private func requestStudyDetailData() {
        Network.School
            .studyDetail(id: id)
            .request()
            .responseData(StudyDetailModel.self) { [weak self] model in
                guard let `self` = self else { return }
                self.navigation.item.title = model.result.title
                self.cateId = model.result.interactLesson.resourceId
                self.imageBaseName = model.result.interactLesson.imageBaseName
                self.requestStudySecData()
        } failure: { error in
            Toast.show(info: error.errorMessage)
        }
    }
    
    private func requestStudySecData() {
        Network.School
            .studySec(cateId: cateId)
            .request()
            .responseData(StudySecModel.self) { [weak self] model in
                guard let `self` = self else { return }
                self.titles = model.result.items
                self.reloadData()
        } failure: { error in
            Toast.show(info: error.errorMessage)
        }
    }
}

// MARK: JXCategoryViewDelegate
extension StudyViewController: JXCategoryViewDelegate {
    
    func categoryView(_ categoryView: JXCategoryBaseView!, didClickSelectedItemAt index: Int) {
        listContainerView.didClickSelectedItem(at: index)
    }
}

// MARK: JXCategoryListContainerViewDelegate
extension StudyViewController: JXCategoryListContainerViewDelegate {
    
    func number(ofListsInlistContainerView listContainerView: JXCategoryListContainerView!) -> Int {
        return controllers.count
    }
    
    func listContainerView(_ listContainerView: JXCategoryListContainerView!, initListFor index: Int) -> JXCategoryListContentViewDelegate! {
        return controllers[index]
    }
}
